package com.gjj.abox.custom;

import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gjj.abox.database.R;
import com.gjj.abox.utils.AnimalUtils;

/**
 * Created by DWCloud on 2016/3/31.
 */
public class MessageDialog {

    /**
     * 获取屏幕分辨率宽
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    /**
     * 获取屏幕分辨率高
     */
    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.heightPixels;
    }

    /*
       *确认提示对话框
       */
    public static android.app.Dialog showConfirmDialog(Context context, String content, final DialogConfirmClickListener dialogConfirmClickListener) {
        return ShowConfirmDialog(context, content, dialogConfirmClickListener);
    }

    /*
       *提示对话框
       */
    public static android.app.Dialog showTipsDialog(Context context, String title, String tips) {
        return ShowTipsDialog(context, title, tips);
    }

    /*
      *显示带输入的对话框
      */
    public static android.app.Dialog showInputDialog(Context context, String title,String content, final InputDialogConfirmClickListener inputDialogConfirmClickListener) {
        return ShowInputDialog(context, title,content, inputDialogConfirmClickListener);
    }

    /*
     *底部的dialog
     */
    public static android.app.Dialog showBottomMenu(Context context, String[] str, final MenuDialogOnItemClickListener menuDialogClickListener) {
        return ShowBottomMenuDialog(context, str, menuDialogClickListener);
    }


    private static android.app.Dialog ShowConfirmDialog(Context context, String toast, final DialogConfirmClickListener dialogConfirmClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(false);
        View view = LayoutInflater.from(context).inflate(R.layout.true_false_dialog, null);
        dialog.setContentView(view);
        ((TextView) view.findViewById(R.id.tv_true_false_content)).setText(toast);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_true_false_cancel);
        TextView tvConfirm = (TextView) view.findViewById(R.id.tv_true_false_sure);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                dialogConfirmClickListener.confirm();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = getScreenHeight(context) / 3 * 2;
        } else {
            lp.width = getScreenWidth(context) / 3 * 2;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    private static android.app.Dialog ShowTipsDialog(Context context, String title, String tips) {
        final android.app.Dialog dialog = new android.app.Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.tips_dialog, null);
        dialog.setContentView(view);
        ((TextView) view.findViewById(R.id.tv_tip_dialog_title)).setText(title);
        ((TextView) view.findViewById(R.id.tv_tip_dialog_content)).setText(tips);

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = getScreenHeight(context) / 3 * 2;
        } else {
            lp.width = getScreenWidth(context) / 3 * 2;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }


    private static android.app.Dialog ShowInputDialog(Context context, String title,String content, final InputDialogConfirmClickListener inputDialogConfirmClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(false);
        View view = LayoutInflater.from(context).inflate(R.layout.input_dialog, null);
        dialog.setContentView(view);
        ((TextView) view.findViewById(R.id.tv_input_dialog_title)).setText(title);
        final EditText etInput = (EditText) view.findViewById(R.id.ed_input_dialog_content);
        etInput.setText(content);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_input_dialog_cancel);
        TextView tvConfirm = (TextView) view.findViewById(R.id.tv_input_dialog_sure);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(etInput.getText().toString().trim().equals("")){
                    AnimalUtils.shakeAnimal(etInput);
                }else{
                    dialog.dismiss();
                    inputDialogConfirmClickListener.confirm(etInput.getText().toString().trim());
                }
            }
        });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = getScreenHeight(context) / 3 * 2;
        } else {
            lp.width = getScreenWidth(context) / 6 * 4;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    private static android.app.Dialog ShowBottomMenuDialog(Context context, String[] str, final MenuDialogOnItemClickListener menuDialogClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_popupwindow_bottom_menu, null);
        dialog.setContentView(view);
        TextView menu4 = (TextView) view.findViewById(R.id.pop_5);
        TextView menu3 = (TextView) view.findViewById(R.id.pop_4);
        TextView menu2 = (TextView) view.findViewById(R.id.pop_2);
        TextView menu1 = (TextView) view.findViewById(R.id.pop_1);
        TextView missBtn = (TextView) view.findViewById(R.id.pop_3);
        switch (str.length) {
            case 1:
                menu1.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                break;
            case 2:
                menu1.setVisibility(View.VISIBLE);
                menu2.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                menu2.setText(str[1]);
                break;
            case 3:
                menu1.setVisibility(View.VISIBLE);
                menu2.setVisibility(View.VISIBLE);
                menu3.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                menu2.setText(str[1]);
                menu3.setText(str[2]);
                break;
            case 4:
                menu1.setVisibility(View.VISIBLE);
                menu2.setVisibility(View.VISIBLE);
                menu3.setVisibility(View.VISIBLE);
                menu4.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                menu2.setText(str[1]);
                menu3.setText(str[2]);
                menu4.setText(str[3]);
        }
        menu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(1);
                dialog.dismiss();
            }
        });
        menu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(2);
                dialog.dismiss();
            }
        });
        menu3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(3);
                dialog.dismiss();
            }
        });
        menu4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(4);
                dialog.dismiss();
            }
        });
        missBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = getScreenHeight(context);
        } else {
            lp.width = getScreenWidth(context);
        }
        mWindow.setAttributes(lp);
        mWindow.setGravity(Gravity.BOTTOM);
        mWindow.setWindowAnimations(R.style.Menu_PopuWindow_style);
        dialog.show();
        return dialog;
    }

    public interface DialogConfirmClickListener {
        //        public abstract void cancel();
        public abstract void confirm();
    }

    public interface InputDialogConfirmClickListener {
        //        public abstract void cancel();
        public abstract void confirm(String content);
    }

    public interface MenuDialogOnItemClickListener {
        public abstract void onItemClick(int index);
    }
}
